﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Text;

namespace PowerBooking.RSE.Utility.Helpers
{
    /// <summary>
    /// 缓存辅助类
    /// </summary>
    public class CacheHelper
    {
        private static ObjectCache cache { get; set; }

        static CacheHelper()
        {
            cache = MemoryCache.Default;
        }

        public static void Add(string key, object value, DateTime expired)
        {
            var cacheItem = new CacheItem(key, value);
            var cacheItemPolicy = new CacheItemPolicy() { AbsoluteExpiration = expired };
            cache.AddOrGetExisting(cacheItem, cacheItemPolicy);
        }

        public static void Set(string key, object value, DateTime expired)
        {
            var cacheItem = new CacheItem(key, value);
            var cacheItemPolicy = new CacheItemPolicy() { AbsoluteExpiration = expired };
            cache.Set(cacheItem, cacheItemPolicy);
        }

        public static T Get<T>(string key)
        {
            return (T)cache.Get(key);
        }
    }
}
